<template>
  <el-dialog
    :title="!dataForm.punchId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="学生用户id" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="学生用户id"></el-input>
    </el-form-item>
    <el-form-item label="打卡日期" prop="punchDate">
      <el-input v-model="dataForm.punchDate" placeholder="打卡日期"></el-input>
    </el-form-item>
    <el-form-item label="打卡时间" prop="punchTime">
      <el-input v-model="dataForm.punchTime" placeholder="打卡时间"></el-input>
    </el-form-item>
    <el-form-item label="打卡状态1是打卡0是未打卡" prop="punchStatus">
      <el-input v-model="dataForm.punchStatus" placeholder="打卡状态1是打卡0是未打卡"></el-input>
    </el-form-item>
    <el-form-item label="打卡所在地-拓展字段" prop="punchLocation">
      <el-input v-model="dataForm.punchLocation" placeholder="打卡所在地-拓展字段"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          punchId: 0,
          userId: '',
          punchDate: '',
          punchTime: '',
          punchStatus: '',
          punchLocation: ''
        },
        dataRule: {
          userId: [
            { required: true, message: '学生用户id不能为空', trigger: 'blur' }
          ],
          punchDate: [
            { required: true, message: '打卡日期不能为空', trigger: 'blur' }
          ],
          punchTime: [
            { required: true, message: '打卡时间不能为空', trigger: 'blur' }
          ],
          punchStatus: [
            { required: true, message: '打卡状态1是打卡0是未打卡不能为空', trigger: 'blur' }
          ],
          punchLocation: [
            { required: true, message: '打卡所在地-拓展字段不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.punchId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.punchId) {
            this.$http({
              url: this.$http.adornUrl(`/generator/punch/info/${this.dataForm.punchId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userId = data.punch.userId
                this.dataForm.punchDate = data.punch.punchDate
                this.dataForm.punchTime = data.punch.punchTime
                this.dataForm.punchStatus = data.punch.punchStatus
                this.dataForm.punchLocation = data.punch.punchLocation
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/punch/${!this.dataForm.punchId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'punchId': this.dataForm.punchId || undefined,
                'userId': this.dataForm.userId,
                'punchDate': this.dataForm.punchDate,
                'punchTime': this.dataForm.punchTime,
                'punchStatus': this.dataForm.punchStatus,
                'punchLocation': this.dataForm.punchLocation
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
